package com.dingyh.shopwebbackground.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingyh.shopwebbackground.entity.Payouts;
import com.dingyh.shopwebbackground.mapper.PayoutsMapper;
import com.dingyh.shopwebbackground.service.PayoutsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingyh.shopwebbackground.vo.LineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-21
 */
@Service
public class PayoutsServiceImpl extends ServiceImpl<PayoutsMapper, Payouts> implements PayoutsService {

    private static final String PATTERN = "MM";

    private static final String[] DATE_ARRAY = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};


    @Autowired
    PayoutsMapper payoutsMapper;

    public List<Payouts> findList(Long startTime, Long endTime) {
        LambdaQueryWrapper<Payouts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Payouts::getTimeadd, startTime)
                .le(Payouts::getTimeadd, endTime);
        return list(queryWrapper);
    }


    public Map getLine(Long startTime, Long endTime){
        List<Payouts> list = findList(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.EMPTY_MAP;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
        list.forEach(payouts -> payouts.setMonth(sdf.format(payouts.getTimeadd())));

        List<LineVO> lineVOList = new ArrayList<>();

        Map<String, List<Payouts>> map = list.stream().collect(Collectors.groupingBy(Payouts::getSource));
        map.forEach((k, v) -> {
            BigDecimal[] array = new BigDecimal[12];
            Map<Integer, BigDecimal> moneyMap = new HashMap<>();
            v.forEach(p -> {
                String month = p.getMonth();
                BigDecimal money = Optional.ofNullable(p.getMoney()).orElse(BigDecimal.ZERO);
                BigDecimal totalMoney = Optional.ofNullable(moneyMap.get(month)).orElse(BigDecimal.ZERO);
                moneyMap.put(Integer.valueOf(month), totalMoney.add(money));
            });
            for (int i = 0; i < 12; i++) {
                array[i] = Optional.ofNullable(moneyMap.get(i+1)).orElse(BigDecimal.ZERO);
            }
            LineVO lineVO = LineVO.builder().name(k).moneyArray(array).build();
            lineVOList.add(lineVO);
        });
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("xAxis", DATE_ARRAY);
        resultMap.put("series", lineVOList);
        return resultMap;
    }

    @Override
    public List<Payouts>  getAllPayouts() {
        return  payoutsMapper.getAllPayouts();
    }



}
